#include <unistd.h>
#include "AnnularQuque.hpp"
using namespace SemModule;

struct ThreadData
{
    // ThreadData(){}
    AnQueue<int> *rq;
    std::string name;
    // pthread_t tid;
};

void* consumerrun(void* args)
{
    int data = 1;
    ThreadData* t  = static_cast<ThreadData* >(args);
    while(true)
    {
        // int data = bq->pop();
        // task_t t = bq->pop();
        sleep(2);
        // int out = bq->;
        // ThreadData * t;
        int t1 = t->rq->Pop();
        std::cout << t->name << "消费者消费了一个任务" << t1 << std::endl;
        // t();
        // data++;
    }
}

void* productorrun(void* args)
{
    int data=1;
    ThreadData* bq = static_cast<ThreadData* >(args);
    while(true)
    {
        // bq->EnterQueue()
        // std::cout << "消费者消费了一个任务" << std::endl;
        // bq->pop();
        // sleep(1);
        // bq->EnterQueue(data);
        // sleep(1);
        std::cout << bq->name << "生产了一个任务" << data << std::endl;
        // bq->EnterQueue(1);
        
        bq->rq->EnterQueue(data);

        data++;

        // bq->EnterQueue(bq->);
        // data++;
        // bq->EnterQueue(upload);
    }
}

int main()
{
    AnQueue<int>* bq = new AnQueue<int>();
    // AnQueue<ThreadData*>* aq = new AnQueue<ThreadData*>();
    pthread_t consumer[2],productor[3];

    ThreadData* td1 = new ThreadData();
    td1->name = "productor-1";
    td1->rq = bq;
    // td1->tid = 1; 

    pthread_create(productor,nullptr,productorrun,td1);

    ThreadData* td2 = new ThreadData();
    td2->name = "productor-2";
    // td2->tid = 2;
    td2->rq = bq; 
    pthread_create(productor + 1,nullptr,productorrun,td2);

    ThreadData* td3 = new ThreadData();
    td3->name = "productor-3";
    // td3->tid = 3; 
    td3->rq = bq;
    pthread_create(productor + 2,nullptr,productorrun,td3);


    ThreadData* td4 = new ThreadData();
    td4->name = "consumer-1";
    // td4->tid = 1;
    td4->rq = bq;
    pthread_create(consumer,nullptr,consumerrun,td4);
    

    ThreadData* td5 = new ThreadData();
    td5->name = "consumer-2";
    // td5->tid = 1;

    td5->rq = bq;
    pthread_create(consumer + 1,nullptr,consumerrun,td5);



    pthread_join(consumer[0],nullptr);
    pthread_join(consumer[1],nullptr);

    pthread_join(productor[0],nullptr);
    pthread_join(productor[1],nullptr);
    pthread_join(productor[2],nullptr);
    return 0;
}